package com.j3a.journal.generic;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.springframework.transaction.annotation.Transactional;

@Transactional( readOnly = true )
public abstract class ServiceGeneric<T, PK extends Serializable> implements IServiceGeneric<T, PK> {

        public abstract IDaoGeneric<T, PK> getDAO();

        @Override
        @Transactional( readOnly = false )
        public synchronized T ajouter( T t ) {
                T t1 = this.getDAO().ajouter( t );
                return t1;
        }

        @Override
        @Transactional( readOnly = false )
        public synchronized T modifier( T t ) {
                T t1 = this.getDAO().modifier( t );
                return t1;
        }

        @Override
        @Transactional( readOnly = false )
        public boolean supprimer( T t ) {
                try {
                        this.getDAO().supprimer( t );
                        return true;
                } catch ( Exception ex ) {
                        Logger.getLogger( ServiceGeneric.class.getName() ).log( Level.SEVERE, null, ex );
                        return false;
                }
        }

        @SuppressWarnings( "unchecked" )
        @Override
        public List<T> lister() {
                try {
                        List<T> entites = this.getDAO().lister();
                        return entites;
                } catch ( Exception ex ) {
                        Logger.getLogger( ServiceGeneric.class.getName() ).log( Level.SEVERE, null, ex );
                        return Collections.EMPTY_LIST;
                }
        }

        @Override
        public T selectionner( PK k ) {
                T t1 = this.getDAO().selectionner( k );
                return t1;
        }

}
